package com.troyhigh.njrotc.admintracker;

import java.io.Serializable;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * A class to store information about an event as well as its roster.
 * 
 * @author Quan Nguyen
 * @version 2008-December-23
 */
public class Event implements Serializable
{   
    private String name;
    private Date date;
    private Category category;
    private int duration;
    private Hashtable<Integer, Integer> table;
    
    //I've sometimes had problems dealing with a really bad table system. This
    //way, it's impossible for a table to be null.
    public Event(String n, Date d, Category c, int t, Hashtable<Integer, Integer> r)
    {
        name = n;
        date = d;
        category = c;
        duration = t;
        table = (r != null ? r : new Hashtable<Integer, Integer>() );
    }
    
    public Event(String n, Date d, Category c, int t)
    {
        this(n, d, c, t, new Hashtable<Integer, Integer>());
    }
    
    //Now we have some more accessors.
    public String getName() { return name; }
    public Date getDate() { return date; }
    public Category getCategory() { return category; }
    public int getDuration() { return duration; }
    
    @Override
    public String toString()
    {
        return name + " (" + category.toString() + "), " + date.toString();
    }
    
    @Override
    public boolean equals(Object obj)
    {
        Event that = ((Event)obj);
        return this.name.equals(that.name) &&
               this.date.equals(that.date) &&
               this.category.equals(that.category) &&
               this.duration == that.duration &&
               this.table.equals(that.table);
    }
    
    @Override
    public int hashCode()
    {
        return name.hashCode() +
               date.hashCode() +
               category.ordinal() * 1024 +
               duration * 2048 +
               table.hashCode();
    }
    
    public String getRosterID() //This String identifies the event's roster. Do not change!
    {
         return AdminTracker.rosterID(name, date);
    }
    
    //One cannot simply get the Table. That should be left to internal functions.
    //But I will need to set the table, as when I retrieve my rosters. Check for
    //dependencies on below method later.
    public void setTable(Hashtable<Integer, Integer> t)
    {
        if(t != null) table = t; //If t = null, do not change.
    }
    
    //Now we get the table methods. Set == add, set, remove.
    public void set(Cadet c, int dur)
    {
        set(c.getID(), dur);
    }
    
    public void set(int id, int dur)
    {
        table.put(new Integer(id), new Integer(dur));
    }
    
    //This method allows us to wipe the trace of a cadet.
    public void rem(int id)
    {
        table.remove(new Integer(id));
    }
    
    public ArrayList<Integer> getIDs() //Used for databasing purposes.
    {
        ArrayList<Integer> r = new ArrayList<Integer>();
        Enumeration<Integer> e = table.keys();
        while(e.hasMoreElements())
            r.add(e.nextElement());
        return r;
    }
    
    public int get(int id)
    {
        Integer value = table.get(id);
        if(value == null) return 0;
        return value.intValue();
    }   
}